﻿/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

************************************************************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Votations.NSurvey.WebAdmin.UserControls;
using Votations.NSurvey;
using Votations.NSurvey.Data;
using Votations.NSurvey.DataAccess;
using Votations.NSurvey.BusinessRules;

namespace Votations.NSurvey.WebAdmin
{
    /// <summary>
    /// Handle all the updates of the survey data
    /// </summary>
    public partial class SurveyPrivacy : PageBase
    {

        public override string ActionCode
        {
            get
            {
                return "100";//新建调查报告
            }
        }
        protected void Page_Load(object sender, System.EventArgs e)
        {
            SetupSecurity();
            LocalizePage();
            SurveyMessageConditons.SurveyId = SurveyId;

            if (!Page.IsPostBack)
            {
                SurveyList.BindDropDown();
              
                BindFields();
            }

        }

        private void SetupSecurity()
        {
            CheckRight(NSurveyRights.AccessPrivacySettings, true);
        }

        private void LocalizePage()
        {
            PrivacySettingsTitle.Text = GetPageResource("PrivacySettingsTitle");
            RedirectionURLLabel.Text = GetPageResource("RedirectionURLLabel");
            ApplyPrivacyButton.Text = GetPageResource("ApplyPrivacyButton");
            ThanksMessageConditionTitle.Text = GetPageResource("ThanksMessageConditionTitle");
            AddNewConditionHyperLink.Text = GetPageResource("AddNewConditionHyperLink");
            AddNewConditionHyperLink.NavigateUrl = string.Format("{0}?surveyid={1}&menuindex={2}",
                UINavigator.MessageConditionEditorHyperLink, SurveyId, MenuIndex);
            EvaluationMessageConditionInfo.Text = GetPageResource("EvaluationMessageConditionInfo");
            ThanksMessageLabel.Text = GetPageResource("ThanksMessageLabel");
        }

        /// <summary>
        /// Get the current DB data and fill 
        /// the fields with them
        /// </summary>
        private void BindFields()
        {
            SurveyList.SurveyId = SurveyId;
            SurveyList.BindDropDown();

            // Retrieve the survey data
            SurveyData surveyData = new Surveys().GetSurveyById(SurveyId);
            SurveyData.SurveysRow survey = surveyData.Surveys[0];

            RedirectionURLTextBox.Text = survey.RedirectionURL;
            ThankYouFreeTextBox.Text = survey.ThankYouMessage;

            SurveyMessageConditons.BindData();
        }


 

        protected void ApplyPrivacyButton_Click(object sender, System.EventArgs e)
        {
            SurveyData surveyData = new Surveys().GetSurveyById(SurveyId);
            surveyData.Surveys[0].RedirectionURL = RedirectionURLTextBox.Text;
            surveyData.Surveys[0].ThankYouMessage = ThankYouFreeTextBox.Text.Length > 3900 ?
                ThankYouFreeTextBox.Text.Substring(0, 3900) : ThankYouFreeTextBox.Text;

            // Update the DB
            new Survey().UpdateSurvey(surveyData);

            MessageLabel.Text = GetPageResource("SurveyUpdatedMessage");
            MessageLabel.Visible = true;
        }


    }

}
